package com.nest4cloud.nest.account.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nest4cloud.nest.account.entity.AccCategoriesEntity;
import com.nest4cloud.nest.account.mapper.AccCategoriesMapper;
import com.nest4cloud.nest.account.service.AccCategoriesService;
import com.nest4cloud.nest.account.vo.AccCategoriesVo;
import com.nest4cloud.nest.common.security.util.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 账单分类
 *
 * @author wens
 * @date 2024-11-25 11:38:30
 */
@Service
public class AccCategoriesServiceImpl extends ServiceImpl<AccCategoriesMapper, AccCategoriesEntity> implements AccCategoriesService {

	@Override
	public boolean saveEntity(AccCategoriesEntity accCategories) {
		accCategories.setUserId(SecurityUtils.getUser().getId());
		return this.save(accCategories);
	}

	/**
	 * 根据登录用户id和分类类型查询分类数据
	 *
	 * @return 分类数据
	 */
	@Override
	public List<AccCategoriesVo> selectUserAccCategories(AccCategoriesEntity accCategories) {
		Long userId = SecurityUtils.getUser().getId();
		List<AccCategoriesEntity> accCategoriesEntities = this.list(Wrappers.<AccCategoriesEntity>query().lambda()
//				.eq(AccCategoriesEntity::getUserId, userId)
				.eq(AccCategoriesEntity::getCategoryType, accCategories.getCategoryType()));
		if (accCategoriesEntities!= null && !accCategoriesEntities.isEmpty()) {
			return accCategoriesEntities.stream().map(ele -> new AccCategoriesVo(ele.getId(), ele.getName(), ele.getIcon())).collect(Collectors.toList());
		}
		return Collections.emptyList();
	}
}
